package com.alien.admin.context.task.impl;

import com.alien.admin.context.task.DeployStatusTimeline;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: zsy
 * @Date: 2021/10/29 11:08
 */
@Component
public class DeployStatusTimelineImpl implements DeployStatusTimeline {

    private final String timeLineKeyPrefix = "deploy:timeline:";

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public void putRecId(Long recId, Long expiredTime) {
        String key = String.format("%s%d", timeLineKeyPrefix, recId);
        String requestId = UUID.randomUUID().toString();
        redisTemplate.opsForValue().setIfAbsent(key, requestId, expiredTime, TimeUnit.SECONDS);
    }

    @Override
    public Boolean isAlive(Long recId) {
        String key = String.format("%s%d", timeLineKeyPrefix, recId);
        return redisTemplate.hasKey(key);
    }
}
